package com.lhf.springcloud.controller.teacher;

import com.lhf.springcloud.Entity.teacher.TeacherData;
import com.lhf.springcloud.service.TeacherCompleteDataService;
import com.lhf.springcloud.utils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/teacherCompleteData")
public class TeacherCompleteDataController {

    @Autowired
    private TeacherCompleteDataService teacherCompleteData;
    /**
     * 加载老师基本信息
     * @return
     */
    @GetMapping("/teacherCompleteDataHtml")
    public String loadTeacherCompleteData(){
        return "project/dataCenter/teacher/teacherEssentialInformation";
    }
    /**
     * 分页查询教职工信息
     */
    @RequestMapping(value = "/findAllTeacherData",method = RequestMethod.GET)
    @ResponseBody
    public Map findAllTeacherData(Integer page,Integer limit){
        if (page == null){
            page =1;
        }
        if (limit == null){
            page =10;
        }
        Map allTeacherBaseData = teacherCompleteData.findAllTeacherBaseData(page,limit);
        Map map = new HashMap<>();
        map.put("code",0);
        map.put("msg","success");
        map.put("count",allTeacherBaseData.get("count"));
        map.put("data",allTeacherBaseData.get("data"));
        return map;
    }
    /**
     * 根据身份证号查询教职工信息
     */
    @GetMapping( "/findByIdCard")
    @ResponseBody
    public Map findByIdCard(String idCard){
        List<TeacherData> oneTeacherData = teacherCompleteData.findIdCard(idCard);
        Map map = new HashMap<>();
        map.put("code",0);
        map.put("msg","success");
        map.put("count",oneTeacherData.size());
        map.put("data",oneTeacherData);
        return map;
    }
    /**
     * 更新数据
     */
    @RequestMapping("/updateTeacherData")
    public String updateTeacherData(String value,String id,String field){
        teacherCompleteData.updateTeaherData(value,id,field);
        return "redirect:/teacherCompleteData/teacherCompleteDataHtml";
    }
    /**
     * 文件上传
     */
    @PostMapping("/upLoadTeacherEssentialInformation")
    public String HandleFileUpload(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes){
        if (file.isEmpty()){
            return "redirect:/teacherEssentialInformation";
        }
            List<TeacherData> list1 = new ArrayList<>();
            String s = null;

        try {
            InputStream inputStream = file.getInputStream();
            List<List<Object>> list = ExcelUtils.getCourseListByExcel(inputStream, file.getOriginalFilename());
            inputStream.close();

            for (int i = 1; i < list.size(); i++) {
                List<Object> courseList = list.get(i);

                TeacherData course = new TeacherData();
                if (courseList.size() == 0) {
                    break;

                }
                course.setTeacherName(courseList.get(0).toString());
                course.setIdCard(courseList.get(1).toString());
                course.setJobNumber(courseList.get(2).toString());
                course.setGender(courseList.get(3).toString());
                course.setAge(courseList.get(4).toString());
                course.setPhotoUrl(courseList.get(5).toString());
                course.setNation(courseList.get(6).toString());
                course.setMating(courseList.get(7).toString());
                course.setPoliticalAffiliation(courseList.get(8).toString());
                course.setAdministrationOffice(courseList.get(9).toString());
                course.setAdministrativeDepartment(courseList.get(10).toString());
                course.setEmployeeCategory(courseList.get(11).toString());
                course.setJobAge(courseList.get(12).toString());
                course.setPosition(courseList.get(13).toString());
                course.setPost(courseList.get(14).toString());
                course.setRank(courseList.get(15).toString());
                course.setCoursesOffered(courseList.get(16).toString());
                course.setJoinTime(courseList.get(17).toString());
                course.setCorrectionDate(courseList.get(18).toString());
                course.setDepartureDate(courseList.get(19).toString());
                course.setServiceLife(courseList.get(20).toString());
                course.setHighestEducation(courseList.get(21).toString());
                course.setGraduateSchool(courseList.get(22).toString());
                course.setMajor(courseList.get(23).toString());
                course.setAcademicDegree(courseList.get(24).toString());
                course.setEducationNature(courseList.get(25).toString());
                course.setGraduationTime(courseList.get(26).toString());
                course.setHomeAddress(courseList.get(27).toString());
                course.setContactNumber(courseList.get(28).toString());
                course.setMailbox(courseList.get(29).toString());
                course.setEmergencyContact(courseList.get(30).toString());
                course.setRelationship(courseList.get(31).toString());
                course.setEmergencyContactNumber(courseList.get(32).toString());
                course.setAccountCharacter(courseList.get(33).toString());
                course.setCertificate(courseList.get(34).toString());
                course.setCertificateTime(courseList.get(35).toString());
                course.setTitle(courseList.get(36).toString());
                course.setTitleTime(courseList.get(37).toString());
                course.setOtherTitle(courseList.get(38).toString());
                course.setSocialSecurityAccount(courseList.get(39).toString());
                course.setPurchaseDate(courseList.get(40).toString());
                course.setStopDate(courseList.get(41).toString());
                course.setProvidentFundAccountNumber(courseList.get(42).toString());
                course.setProvidentFundAccountNumberPurchaseDate(courseList.get(43).toString());
                course.setProvidentFundAccountNumberStopDate(courseList.get(44).toString());
                course.setContractType(courseList.get(45).toString());
                course.setContractNumber(courseList.get(46).toString());
                course.setContractStartTime(courseList.get(47).toString());
                course.setContractEndTime(courseList.get(48).toString());
                course.setSalary(courseList.get(49).toString());
                course.setSalaryChange(courseList.get(50).toString());
                course.setTransferRecord(courseList.get(51).toString());
                course.setAccommodation(courseList.get(52).toString());
                course.setRoomNumber(courseList.get(53).toString());
                course.setStartWorkingInUniversitiesTime(courseList.get(54).toString());
                course.setStartAdministrationWorkingInUniversitiesTime(courseList.get(55).toString());
                course.setInfoJudge(courseList.get(56).toString());
                list1.add(course);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (list1.size()>0){
            s = teacherCompleteData.addCourse(list1);
        }
        redirectAttributes.addFlashAttribute("message",s);
        return "redirect:/teacherEssentialInformation";
    }
    /**
     * 插入测试数据api
     */
    @RequestMapping(value = "/isnert",method = RequestMethod.GET)
    @ResponseBody
    public void insert(){
        TeacherData teacherData = new TeacherData();
        this.teacherCompleteData.insert(teacherData);
    }
}
